//linux/kernel/blk_drv/ramdisk.c
#include <string.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/kernel.h>


#define MAJOR_NR 1
#include "blk.h"
#define RD_INODE_BLOCKS 30
char *rd_start;
int	rd_length = 0;

void do_rd_request(void)
{
	int len;
	char *addr;
	INIT_REQUEST;
	addr = rd_start + (CURRENT->sector << 9);
	len = CURRENT->nr_sectors << 9;
	if((MINOR(CURRENT->dev) != 1)||(addr+len > rd_start+rd_length)){
		end_request(0);
		goto repeat;
	}
	if(CURRENT->cmd == WRITE){
		memcpy(addr, CURRENT->buffer, len);
	}else if(CURRENT->cmd == READ){
		memcpy(CURRENT->buffer, addr, len);
	}else
		panic("unknow ramdisk-command");
	end_request(1);
	goto repeat;
}
	
// 为ramdisk 分配内存：从mem_start 开始的length字节
// 返回值：length
// 描述：
// 	1.设置blk_dev[1].request_fn = do_rd_request
// 	2.将mem_start开始的length字节内容清零
long rd_init(long mem_start, int length) 
{
	blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
	rd_start = (char *)mem_start;
	rd_length = length;
	memset(rd_start, 0, rd_length);
	return length;
}

void rd_load(void)
{
	if(!rd_length)
		return;
	printk("[kernel]:RAM Disk:%dK, starting at 0x%x\n\r",rd_length/1024,(int)rd_start);
	if(MAJOR(ROOT_DEV)!=2)
		return;
	return;
}
	
